package com.dalongxin.work.data;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;


import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@DatabaseTable(tableName="department")
public class Department extends SelectionBase {

    @DatabaseField
    private int companyId;
    @DatabaseField
    private String companyName;
    @DatabaseField
    private int departmentId;
    @DatabaseField
    private String departmentName;

    private List<Contact> userList;

    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public int getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public List<Contact> getUserList() {
        return userList;
    }

    public void setUserList(List<Contact> userList) {
        this.userList = userList;
    }

    //---------------通讯录里的解析------------------//
    public static Department parseFromJson(JSONObject jsonObject) {
        Department department = new Department();
        department.setId(jsonObject.optInt("departmentId"));
        department.setName(jsonObject.optString("departmentName"));
        //以上2个字段为了在选择的时候复用
        department.companyId = jsonObject.optInt("companyId");
        department.companyName = jsonObject.optString("companyName");
        department.departmentId = jsonObject.optInt("departmentId");
        department.departmentName = jsonObject.optString("departmentName");
        department.userList = Contact.parseFromJsonList(jsonObject.optJSONArray("userList"));
        return department;
    }

    public static List<Department> parseFromJsonList(JSONArray jsonArray) {
        List<Department> list = new ArrayList<>();
        if(jsonArray != null && jsonArray.length() > 0) {
            for(int i=0;i<jsonArray.length();i++) {
                Department item = parseFromJson(jsonArray.optJSONObject(i));
                if(item != null)list.add(item);
            }
        }
        return list;
    }

    //---------------部门列表里的解析------------------//
    public static Department parseFromJsonInDepartment(JSONObject jsonObject) {
        Department department = new Department();
        department.setId(jsonObject.optInt("departmentId"));
        department.setName(jsonObject.optString("departmentName"));
        //以上2个字段为了在选择的时候复用
        department.companyId = jsonObject.optInt("companyId");
        department.companyName = jsonObject.optString("companyName");
        department.departmentId = jsonObject.optInt("departmentId");
        department.departmentName = jsonObject.optString("departmentName");
        return department;
    }

    public static List<Department> parseFromJsonListInDepartment(JSONArray jsonArray) {
        List<Department> list = new ArrayList<>();
        if(jsonArray != null && jsonArray.length() > 0) {
            for(int i=0;i<jsonArray.length();i++) {
                Department item = parseFromJsonInDepartment(jsonArray.optJSONObject(i));
                if(item != null)list.add(item);
            }
        }
        return list;
    }

    //---------------员工列表里的解析------------------//
    public static List<Contact> parseFromJsonInContact(JSONObject jsonObject) {
        return Contact.parseFromJsonList(jsonObject.optJSONArray("userList"));
    }

    public static List<Contact> parseFromJsonListInContact(JSONArray jsonArray) {
        List<Contact> list = new ArrayList<>();
        if(jsonArray != null && jsonArray.length() > 0) {
            for(int i=0;i<jsonArray.length();i++) {
                List<Contact> items = parseFromJsonInContact(jsonArray.optJSONObject(i));
                if(items != null)list.addAll(items);
            }
        }
        return list;
    }
}
